package com.zretc.device.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.zretc.device.dao.UserDao;
import com.zretc.device.entity.User;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ModPassUser extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtusername;
	private JPasswordField txtpassword;
	private JPasswordField txtpassconfirm;
	private JPasswordField txtBadPass;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			ModPassUser dialog = new ModPassUser();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public ModPassUser() {
		setBounds(100, 100, 693, 488);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel label = new JLabel("修改密码");
		label.setFont(new Font("宋体", Font.PLAIN, 40));
		label.setBounds(216, 28, 167, 47);
		contentPanel.add(label);

		txtusername = new JTextField();
		txtusername.setEditable(false);
		txtusername.setFont(new Font("宋体", Font.PLAIN, 25));
		txtusername.setColumns(10);
		txtusername.setBounds(268, 113, 250, 30);
		contentPanel.add(txtusername);

		JLabel label_1 = new JLabel("用 户 名");
		label_1.setFont(new Font("宋体", Font.PLAIN, 25));
		label_1.setBounds(134, 113, 101, 30);
		contentPanel.add(label_1);

		txtpassword = new JPasswordField();
		txtpassword.setBounds(268, 194, 250, 30);
		contentPanel.add(txtpassword);

		JLabel label_3 = new JLabel("设置新密码");
		label_3.setFont(new Font("宋体", Font.PLAIN, 25));
		label_3.setBounds(110, 194, 125, 30);
		contentPanel.add(label_3);

		JLabel label_4 = new JLabel("确认新密码");
		label_4.setFont(new Font("宋体", Font.PLAIN, 25));
		label_4.setBounds(110, 237, 125, 30);
		contentPanel.add(label_4);

		txtpassconfirm = new JPasswordField();
		txtpassconfirm.setBounds(268, 235, 250, 30);
		contentPanel.add(txtpassconfirm);

		JButton button = new JButton("取消");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtBadPass.setText("");
				txtpassword.setText("");
				txtpassconfirm.setText("");
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 25));
		button.setBounds(161, 312, 150, 38);
		contentPanel.add(button);

		JButton button_1 = new JButton("确认修改");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//取值
				String username = txtusername.getText();
				char [] cbadpass=txtBadPass.getPassword();
				char [] cpassword = txtpassword.getPassword();
				char [] cpassConfirm = txtpassconfirm.getPassword();
				//转型
				String badpass=new String(cbadpass);
				String password = new String(cpassword);
				String passConfirm = new String(cpassConfirm);
				if (password.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "密码不能为空");
				}else if(passConfirm.equals("")){
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "密码不能为空");
				}else {
					//对原密码进行验证
					UserDao ud = new UserDao();
					boolean flag1=ud.dologin(username, badpass);
					if (flag1) {
						if (password.equals(passConfirm)) {
							User user = new User(username, null, password);
							//调用修改密码的方法
							boolean flag=ud.modUser(user);
							if (flag) {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "修改成功");
							}else {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "修改失败");
							}
						}else {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "新密码错误");
						}
					}else {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "原密码错误，修改失败");
					}
					
				}
			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 25));
		button_1.setBounds(368, 312, 150, 38);
		contentPanel.add(button_1);
		
		JLabel label_2 = new JLabel("请输入原密码");
		label_2.setFont(new Font("宋体", Font.PLAIN, 25));
		label_2.setBounds(85, 151, 150, 30);
		contentPanel.add(label_2);
		
		txtBadPass = new JPasswordField();
		txtBadPass.setBounds(268, 151, 250, 30);
		contentPanel.add(txtBadPass);
		
		//给用户名赋值
		User user=User.getInfo();
		txtusername.setText(user.getUsername());
		//居中
		setLocationRelativeTo(null);
	}

	/**
	 * 带参构造
	 */
	public ModPassUser(String username) {
		setBounds(100, 100,  693, 433);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel label = new JLabel("修改密码");
		label.setFont(new Font("宋体", Font.PLAIN, 40));
		label.setBounds(216, 28, 167, 47);
		contentPanel.add(label);

		txtusername = new JTextField();
		// 赋值
		txtusername.setText(username);
		txtusername.setEditable(false);
		txtusername.setFont(new Font("宋体", Font.PLAIN, 25));
		txtusername.setColumns(10);
		txtusername.setBounds(268, 113, 250, 30);
		contentPanel.add(txtusername);

		JLabel label_1 = new JLabel("用 户 名");
		label_1.setFont(new Font("宋体", Font.PLAIN, 25));
		label_1.setBounds(134, 113, 101, 30);
		contentPanel.add(label_1);

		txtpassword = new JPasswordField();
		txtpassword.setBounds(268, 156, 250, 30);
		contentPanel.add(txtpassword);

		JLabel label_3 = new JLabel("密    码");
		label_3.setFont(new Font("宋体", Font.PLAIN, 25));
		label_3.setBounds(134, 156, 120, 30);
		contentPanel.add(label_3);

		JLabel label_4 = new JLabel("确认密码");
		label_4.setFont(new Font("宋体", Font.PLAIN, 25));
		label_4.setBounds(134, 206, 120, 30);
		contentPanel.add(label_4);

		txtpassconfirm = new JPasswordField();
		txtpassconfirm.setBounds(268, 206, 250, 30);
		contentPanel.add(txtpassconfirm);

		JButton button = new JButton("返回");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 25));
		button.setBounds(148, 279, 150, 38);
		contentPanel.add(button);

		JButton button_1 = new JButton("确认修改");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//取值
				String username = txtusername.getText();
				char [] cpassword = txtpassword.getPassword();
				char [] cpassConfirm = txtpassconfirm.getPassword();
				//转型
				String password = new String(cpassword);
				String passConfirm = new String(cpassConfirm);
				if (password.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "密码不能为空");
				}else if(passConfirm.equals("")){
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "密码不能为空");
				}else {
					if (password.equals(passConfirm)) {
						UserDao ud = new UserDao();
						User user = new User(username, null, password);
						//调用修改密码的方法
						boolean flag=ud.modUser(user);
						if (flag) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "修改成功");
						}else {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "修改失败");
						}
					}
				}
				
			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 25));
		button_1.setBounds(355, 279, 150, 38);
		contentPanel.add(button_1);

		// 居中
		setLocationRelativeTo(null);

	}

}
